﻿using DevelopAssistant.Service;
using ICSharpCode.WinFormsUI.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core.ToolBox
{
    public partial class SnippetEdit : BaseForm
    {
        private int ID;
        private int ParentID;
        private string Command = "编辑";

        public SnippetEdit()
        {
            InitializeComponent();
        }

        public SnippetEdit(int id,string command)
            : this()
        {
            this.ID = id;
            this.ParentID = id;
            this.Command = command;
            this.XTheme = AppSettings.WindowTheme;
            InitializeControls();
        }        

        private void InitializeControls()
        {
            if (this.Command == "Add")
                return;

            using (var db = NORM.DataBase.DataBaseFactory.Default)
            {
                string strSql = "SELECT [ID],[NAME],[Pid],[Snippet],[IsShow],[OrderIndex],[Note],[Define] ";
                strSql += ",[Childs] FROM [T_SnippetTree] ";
                strSql += "WHERE [ID]='" + ID + "'";

                DataTable dt = db.GetDataSet(CommandType.Text, strSql, null).Tables[0];

                foreach (DataRow dr in dt.Rows)
                {
                    var isShow = dr["IsShow"] + "";
                    this.txtName.Text = dr["NAME"] + "";
                    this.txtText.Text = dr["Snippet"] + "";
                    this.chkShow.Checked = isShow == "1" ? true : false;
                }
            }
        }

        private void btnApplyOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {                 
                new MessageDialog(DevelopAssistant.Core.Properties.Resources.warning_32px, "名称不能为空").ShowDialog(this);
                return;
            }

            try
            {
                using (var db = NORM.DataBase.DataBaseFactory.Default)
                {
                    string strSql = string.Empty;
                     
                    switch (Command)
                    {
                        case "Add":
                            strSql = "INSERT INTO [T_SnippetTree] ( [NAME],[Pid],[Snippet],[IsShow],[OrderIndex],[Note],[Define],[Childs] ) ";
                            strSql += "VALUES ( '"+txtName.Text+"','"+ParentID+"','"+txtText.Text+"','1','0','SQL片段','Define','0' ) ;";
                            strSql += System.Environment.NewLine + " ";
                            strSql += "UPDATE [T_SnippetTree] SET Childs=Childs+1 WHERE [ID]='" + ParentID + "'";
                            break;
                        case "Edit":
                            strSql = " UPDATE [T_SnippetTree] SET [NAME]='" + txtName.Text + "', ";
                            strSql += " [Snippet]='" + txtText.Text + "' WHERE [ID]='" + ID + "' ";
                            break;
                    }                   
                    db.Execute(CommandType.Text, strSql, null);
                }
            }
            catch (Exception ex)
            {                 
                new MessageDialog(DevelopAssistant.Core.Properties.Resources.error_32px, ex.Message).ShowDialog(this);
                DevelopAssistant.Common.NLogger.WriteToLine(ex.Message, "错误", DateTime.Now, ex.Source, ex.StackTrace);
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SnippetEdit_Load(object sender, EventArgs e)
        {
            this.panel1.BorderColor = this.XTheme.FormBorderOutterColor;
        }

    }
}
